package com.wzh.mysending.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wzh.mysending.R;
import com.wzh.mysending.base.BaseActivity;
import com.wzh.mysending.utils.EwmUtil;
import com.wzutils.AppUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.update.AppVersion;
import cn.bmob.v3.update.BmobUpdateAgent;

/**
 * 关于
 */
public class AboutActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.e_w_m)
    ImageView eWM;
    @BindView(R.id.ver_no)
    TextView verNo;
    @BindView(R.id.check_layout)
    LinearLayout checkLayout;
    @BindView(R.id.fab)
    TextView fab;
    EwmUtil ewmUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }
    private void initEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        ewmUtil = new EwmUtil();
        setTitleBar(toolbar, "关于");
        verNo.setText("V" + AppUtils.getVersionName(context));
        BmobQuery<AppVersion> bmobQuery =new BmobQuery<>();
        bmobQuery.getObject("d8a8fdc9da", new QueryListener<AppVersion>() {
            @Override
            public void done(AppVersion appVersion, BmobException e) {
                if (e==null){
                    creatEwm(appVersion.getPath().getFileUrl(), R.mipmap.logo);
                    if (!AppUtils.getVersionName(context).equals(appVersion.getVersion())){
                        verNo.setText("有新版本");
                    }


                }else {
                    Log.e("E==",e.toString());
                }
            }
        });


    }
    private void creatEwm(String url, int imageId) {
        Bitmap qrBitmap = ewmUtil.generateBitmap(url, 400, 400);
        Bitmap logoBitmap = BitmapFactory.decodeResource(getResources(), imageId);
        Bitmap bitmap = ewmUtil.addLogo(qrBitmap, logoBitmap);
        eWM.setImageBitmap(bitmap);
    }

    @OnClick(R.id.check_layout)
    public void onClick() {
        BmobUpdateAgent.setUpdateOnlyWifi(false);
        BmobUpdateAgent.update(this);
    }

}
